package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CourseChangesResultVO;
import edu.zqu.instructionalMatters.project.entity.CourseChangesResult;
import edu.zqu.instructionalMatters.project.mapper.CourseChangesResultMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课程变动审核结果 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课程变动审核结果")
public class CourseChangesResultController {
    @Autowired
    private CourseChangesResultMapper courseChangesResultMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private CollegeService collegeService;

    @ApiOperation(value = "获取课程变动审核结果列表")
    @RequestMapping(value = "/courseChangesResult", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseChangesApplicationId", value = "课程变动申请id", paramType = "query", required = false),
            @ApiImplicitParam(name = "checkType", value = "审核类型", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             BigInteger courseChangesApplicationId,
                             Integer checkType
    ) {
        CourseChangesResult courseChangesResult1 = new CourseChangesResult()
                .setCollegeId(collegeId).setCourseChangesApplicationId(courseChangesApplicationId).setCheckType(checkType);//合成 CourseChangesResult 对象
        List<CourseChangesResult> list1 = courseChangesResultMapper.findAll(courseChangesResult1);                         //获取 List<CourseChangesResult>
        List<CourseChangesResultVO> list = new ArrayList<>();                                                              //初始化 List<CourseChangesResultVO>
        for (CourseChangesResult courseChangesResult : list1) {                                                             //循环遍历
            CourseChangesResultVO courseChangesResultVO = new CourseChangesResultVO();                                     //初始化 VO
            BeanUtils.copyProperties(courseChangesResult, courseChangesResultVO);                                           //对象复制赋值
            String collegeName = collegeService.getCollegeName(courseChangesResult.getCollegeId());                        //获取学校名称
            String checkName = userService.getUserName(courseChangesResult.getCheckId());                                  //获取审核人名称
            courseChangesResultVO.setCollegeName(collegeName);                                                             //设置学校名称
            courseChangesResultVO.setCheckName(checkName);                                                                 //设置审核人名称
            list.add(courseChangesResultVO);                                                                               //加入到 list
        }
        return ZQUResult.success(list);                                                                                    //对象成功返回
    }

    @ApiOperation(value = "获取课程变动审核结果信息", response = CourseChangesResultVO.class)
    @RequestMapping(value = "/courseChangesResult/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CourseChangesResult courseChangesResult = courseChangesResultMapper.selectById(id);                        //查出对象
        String collegeName = collegeService.getCollegeName(courseChangesResult.getCollegeId());                    //获取学校名称
        String checkName = userService.getUserName(courseChangesResult.getCheckId());                              //获取审核人名称
        CourseChangesResultVO courseChangesResultVO = new CourseChangesResultVO();                                 //初始化VO
        BeanUtils.copyProperties(courseChangesResult, courseChangesResultVO);                                       //对象复制赋值
        courseChangesResultVO.setCollegeName(collegeName);                                                         //设置学校名称
        courseChangesResultVO.setCheckName(checkName);                                                             //设置审核人名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", courseChangesResultVO);                          //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/courseChangesResult", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseChangesApplicationId", value = "课程变动申请id", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkType", value = "审核类型", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkId", value = "审核人id", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkResult", value = "审核结果", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkDetail", value = "审核详情", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger courseChangesApplicationId,
                         BigInteger checkId,
                         Integer checkType,
                         Integer checkResult,
                         String checkDetail,
                         BigInteger byWho) {
        CourseChangesResult courseChangesResult = new CourseChangesResult();          //初始化数据模型
        courseChangesResult.setCollegeId(collegeId);                                  //设置学校id
        courseChangesResult.setCourseChangesApplicationId(courseChangesApplicationId);//设置课程变动申请id
        courseChangesResult.setCheckId(checkId);                                      //设置审批人id
        courseChangesResult.setCheckType(checkType);                                  //设置审核类型
        courseChangesResult.setCheckResult(checkResult);                              //设置审批结果
        courseChangesResult.setCheckDetail(checkDetail);                              //设置审批详情
        courseChangesResult.setByWho(byWho);                                          //设置操作者
        courseChangesResultMapper.insert(courseChangesResult);                        //执行插入
        return ZQUResult.success();                                                   //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/courseChangesResult/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "courseChangesApplicationId", value = "课程变动申请id", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkType", value = "审核类型", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkId", value = "审核人id", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkResult", value = "审核结果", paramType = "query", required = true),
            @ApiImplicitParam(name = "checkDetail", value = "审核详情", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger courseChangesApplicationId,
                            BigInteger checkId,
                            Integer checkType,
                            Integer checkResult,
                            String checkDetail,
                            BigInteger byWho) {
        CourseChangesResult courseChangesResult = courseChangesResultMapper.selectById(id);//查出旧记录
        courseChangesResult.setByWho(byWho);                                          //修改操作者
        courseChangesResultMapper.updateById(courseChangesResult);                    //更新操作者
        courseChangesResultMapper.deleteById(id);                                     //逻辑删除
        courseChangesResult = new CourseChangesResult();                               //初始化数据模型
        courseChangesResult.setCollegeId(collegeId);                                  //设置学校id
        courseChangesResult.setCourseChangesApplicationId(courseChangesApplicationId);//设置课程变动申请id
        courseChangesResult.setCheckId(checkId);                                      //设置审批人id
        courseChangesResult.setCheckType(checkType);                                  //设置审核类型
        courseChangesResult.setCheckResult(checkResult);                              //设置审批结果
        courseChangesResult.setCheckDetail(checkDetail);                              //设置审批详情
        courseChangesResult.setByWho(byWho);                                          //设置操作者
        courseChangesResult.setLastVersion(id);                                       //设置上一版本
        courseChangesResultMapper.insert(courseChangesResult);                        //执行插入
        return ZQUResult.success();                                                   //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/courseChangesResult/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CourseChangesResult courseChangesResult = courseChangesResultMapper.selectById(id);//查出旧记录
        courseChangesResult.setByWho(byWho);                                          //修改操作者
        courseChangesResultMapper.updateById(courseChangesResult);                    //更新操作者
        courseChangesResultMapper.deleteById(id);                                     //逻辑删除
        return ZQUResult.success();                                                   //操作成功返回
    }
}

